package com.hzmy.zm.dropdonwdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hzmy.zm.dropdonwdemo.R;
import com.hzmy.zm.dropdonwdemo.model.FilterThreeEntity;

import java.util.List;

/**
 * 产品类别 一级菜单的adapter
 */
public class FilterLeftAdapter extends BaseListAdapter<FilterThreeEntity> {

    private FilterThreeEntity selectedEntity;

    public FilterLeftAdapter(Context context) {
        super(context);
    }

    public FilterLeftAdapter(Context context, List<FilterThreeEntity> list) {
        super(context, list);
    }

    /**
     * 设置当前选择的bean
     *
     * @param filterEntity
     */
    public void setSelectedEntity(FilterThreeEntity filterEntity) {
        this.selectedEntity = filterEntity;
        for (FilterThreeEntity entity : getData()) {
            entity.setSelected(entity.getType().equals(selectedEntity.getType()));
        }
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_filter_left, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        FilterThreeEntity entity = getItem(position);
        holder.tvTitle.setText(entity.getType());
        if (entity.isSelected()) {
            holder.tvTitle.setTextColor(mContext.getResources().getColor(R.color.orange));
//            holder.llRootView.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        } else {
            holder.tvTitle.setTextColor(mContext.getResources().getColor(R.color.font_black_2));
//            holder.llRootView.setBackgroundColor(mContext.getResources().getColor(R.color.font_black_6));
        }
        return convertView;
    }

    static class ViewHolder {
        LinearLayout llRootView;
        TextView tvTitle;

        ViewHolder(View view) {
            llRootView = (LinearLayout) view.findViewById(R.id.ll_root_view);
            tvTitle = (TextView) view.findViewById(R.id.tv_title);
        }
    }
}
